package com.bow.algorithm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Parse {

	private static List<String> parse(File file) throws IOException {
		List<String> result = new ArrayList<>();
		FileReader reader = new FileReader(file);
		BufferedReader br = new BufferedReader(reader);
		String header = br.readLine();
		result.add(header);
		String content = null;
		while ((content = br.readLine()) != null) {
			String[] strAry = content.split(",");

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < strAry.length; i++) {
				if (i == 5 && strAry[5].length() != 0) {
					continue;
				}
				sb.append(strAry[i]);
				if (i == strAry.length - 1) {
					continue;
				}
				sb.append(",");
			}
			result.add(sb.toString());
		}
        br.close();
		reader.close();
		return result;
	}

	private static boolean store(List<String> contents, String fileName) throws IOException {
	    String fn = "D:\\data\\update\\"+fileName;
		File file = new File(fn);
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		for (String c : contents) {
			bw.write(c);
			bw.write('\n');
		}
		bw.flush();
		bw.close();
		return true;
	}

    public static void main(String[] args) throws Exception {
        File file = new File("D:\\data");
        for(File f : file.listFiles()){
            List<String> r = parse(f);
            store(r, f.getName());
            System.out.println("finish "+ f.getName());
        }

    }
}
